/*
 * Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
 */

package com.xci.edu.core.service;

import com.github.lvyanyang.annotation.OperateLog;
import com.xci.edu.core.dao.ExamPaperDao;
import com.xci.edu.core.dao.ExamPaperDetailsDao;
import com.xci.edu.core.dao.ExamRecordDetailsDao;
import com.xci.edu.core.entity.ExamPaper;
import com.xci.edu.core.entity.ExamPaperDetails;
import com.xci.edu.core.entity.ExamRecord;
import com.xci.edu.core.dao.ExamRecordDao;
import com.xci.edu.core.entity.ExamRecordDetails;
import com.xci.edu.core.filter.ExamRecordFilter;
import com.github.lvyanyang.core.BaseService;
import com.github.lvyanyang.core.BoolResult;
import com.github.lvyanyang.core.PageList;
import com.github.lvyanyang.core.Param;
import com.github.lvyanyang.core.XCI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 考试记录服务
 * @author 马星俊 maxingjun@xci96716.com
 * @since 2020-11-25 10:52:08
 */
@Service
public class ExamRecordService extends BaseService {

    /** 考试记录数据层对象 */
    @Resource private ExamRecordDao examRecordDao;
    /** 考试记录明细数据层对象 */
    @Resource private ExamRecordDetailsDao examRecordDetailsDao;
    /** 试卷信息数据层对象 */
    @Resource private ExamPaperDao examPaperDao;
    /** 试卷试题明细数据层对象 */
    @Resource private ExamPaperDetailsDao examPaperDetailsDao;

    /**
     * 新建考试记录
     * @param entity 考试记录实体
     */
    @OperateLog(msg = "新建考试记录", module = "考试记录")
    @Transactional(rollbackFor = Exception.class)
    public BoolResult insert(ExamRecord entity) {
        return save(entity, true);
    }

    /**
     * 修改考试记录
     * @param entity 考试记录实体
     */
    @OperateLog(msg = "修改考试记录", module = "考试记录")
    @Transactional(rollbackFor = Exception.class)
    public BoolResult update(ExamRecord entity) {
        return save(entity, false);
    }

    /**
     * 修改考试记录状态
     * @param ids    考试记录主键字符串
     * @param status 考试记录状态
     */
    @OperateLog(msg = "修改考试记录状态", module = "考试记录")
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(@NotBlank(message = "请指定考试记录主键字符串") String ids, @NotNull(message = "请指定考试记录状态") Boolean status) {
        XCI.forEachIds(ids, id -> {
            examRecordDao.updateStatus(id, status);
        });
    }

    /**
     * 删除考试记录
     * @param ids 考试记录主键字符串
     */
    @OperateLog(msg = "删除考试记录", module = "考试记录")
    @Transactional(rollbackFor = Exception.class)
    public BoolResult delete(@NotBlank(message = "请指定考试记录主键字符串") String ids) {
        XCI.forEachIds(ids, id -> {
            var entity = examRecordDao.selectById(id);
            if (entity == null) return;

            examRecordDao.deleteById(entity.getId());
            //logService.saveDeleteHistoryLogAsync(entity.getId(), entity);
        });
        return BoolResult.success();
    }

    /**
     * 根据主键查询单个考试记录
     * @param id 考试记录主键
     * @return 返回考试记录实体
     */
    public ExamRecord selectById(@NotNull(message = "请指定考试记录主键") Long id) {
        return examRecordDao.selectById(id);
    }


    /**
     * 查询考试记录列表
     * @param filter 过滤条件
     * @return 返回考试记录列表
     */
    public List<ExamRecord> selectList(ExamRecordFilter filter) {
        return examRecordDao.selectList(filter);
    }

    /**
     * 查询考试记录分页列表
     * @param filter 过滤条件
     * @return 返回考试记录分页列表
     */
    public PageList<ExamRecord> selectPageList(ExamRecordFilter filter) {
        return PageList.of(examRecordDao.selectPageList(filter));
    }

    /**
     * 保存数据,在insert和update之前和之后的校验和处理
     * @param entity  考试记录实体
     * @param created 是否新建
     */
    private BoolResult save(ExamRecord entity, boolean created) {
        //新建时如果主键未指定,自动设置主键
        XCI.ifTrueAction(XCI.invalidId(entity.getId(), created), () -> entity.setId(XCI.nextId()));

        //更新数据库
        if (created) {
            entity.setExamStartDateTime(XCI.now());
            /** 生成考试记录start */
            examRecordDao.insert(entity);
            /** 生成考试记录end */
            //logService.saveInsertHistoryLogAsync(entity.getId(), entity);
        } else {
            //var before = examRecordDao.selectById(entity.getId());
            examRecordDao.update(entity);
            //logService.saveUpdateHistoryLogAsync(entity.getId(), before, entity);
        }
        return BoolResult.success(entity);
    }

    /**
     * 批量保存考试试题明细
     * @param entities 考试试题明细实体集合
     */
    @OperateLog(msg = "修改考试试题明细", module = "考试试题明细")
    @Transactional(rollbackFor = Exception.class)
    public void batchSave(List<ExamRecordDetails> entities) {
        for (ExamRecordDetails entity : entities) {
            saveDetails(entity, true).ifFailThrow();
        }
    }
    /**
     * 保存考试试题明细数据,在insert和update之前和之后的校验和处理
     * @param entity  考试试题明细实体
     * @param created 是否新建
     */
    private BoolResult saveDetails(ExamRecordDetails entity, boolean created) {

        //更新数据库
        if (created) {
            examRecordDetailsDao.insert(entity);
            //logService.saveInsertHistoryLogAsync(entity.get(), entity);
        } else {
            //var before = examRecordDetailsDao.selectById(entity.get());
            examRecordDetailsDao.update(entity);
            //logService.saveUpdateHistoryLogAsync(entity.get(), before, entity);
        }
        return BoolResult.success();
    }

}